import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class TextReader {
	private List<String> sentences = new ArrayList<>();
	private String text = "";

	public String getText() {
		return text;
	}

	public TextReader(File file) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			while ((line = br.readLine()) != null) {
				text += line + "\n";
			}
			process();
		} catch (IOException e) {
			System.out.println("FAIL. Can't read data file");
		}
	}

	private void process() {
		Pattern p = Pattern.compile("([А-Я\"][^\\.^\\?^!]+[\\.!?])");
		Matcher m = p.matcher(text);

		// List<String> animals = new ArrayList<String>();
		int i = 0;

		String sequence = "";

		while (m.find()) {
			// if (i++ > 100) {
			// System.exit(0);
			// }
			String sentence = m.group();
			sentence = sentence.replaceAll("\\s+", " ");
			sentence = sentence.replaceAll("<|>", "");
			int count = StringUtils.countMatches(sentence, "\"");
			if (count % 2 != 0) {
				if (sequence.length() == 0) {
					sequence = sequence + " " + sentence;
				} else {
					sequence = sequence + " " + sentence;
					sequence = sequence.replaceAll("!\\s+\"", "!\"");
					sequence = sequence.replaceAll("\\?\\s+\"", "?\"");
					sequence = sequence.replaceAll("\\.\\s+\"", ".\"");
					System.out.println("SEQ [" + sequence.trim() + "]");
					sequence = "";
				}
			} else {
				if (sequence.length() > 0) {
					sequence += " " + sentence;
				} else {
					Sentence x = new Sentence(sentence);
					sentences.add(sentence);
					// if (x.getWords().length > 10) {
					// System.out.println("FOUND [" + sentence + "]");
					// }
				}
			}
			// animals.add(m.group());
		}
	}

	public static void main(String[] args) {
		TextReader tr = new TextReader(new File("c:\\temp\\dragon.txt"));
		// System.out.println(tr.getText());
	}
}
